package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;

public class AddItemToCartServlet extends HttpServlet {
    //1.处理完功能后的跳转页面
    private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
    private static final String CHECK_OUT = "/WEB-INF/jsp/cart/Checkout.jsp";

    //2.定义处理该请求所需要的的数据
    private String workingItemId;
    private Cart cart;

    private CartItem cartItem;

    //3.是否需要调用业务逻辑层
    private CatalogService catalogService;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        workingItemId = request.getParameter("workingItemId");
        Account account;
        HttpSession session = request.getSession();
        account = (Account)session.getAttribute("account");
        cart = (Cart)session.getAttribute("cart");
        if(cart == null){
            cart = new Cart();
        }
        if (cart.containsItemId(workingItemId)){
            cart.incrementQuantityByItemId(workingItemId);
            if(account != null){
                HttpServletRequest httpRequest= (HttpServletRequest) request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                LogService logService = new LogService();
                catalogService = new CatalogService();
                Item item = catalogService.getItem(workingItemId);
                String logInfo = logService.logInfo(" ") + strBackUrl + " " + item + "Quantity+1 ";
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
        } else {
            catalogService = new CatalogService();
            boolean isInStock = catalogService.isItemInStock(workingItemId);
            Item item = catalogService.getItem(workingItemId);
            cart.addItem(item,isInStock,1);
            if(account != null){
                HttpServletRequest httpRequest= request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strBackUrl + " Add " + item + " to the cart";
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            if (isInStock) {
                String username = (String)session.getAttribute("username");
                if (username != null) {
                    CartItem cartItem = new CartItem();
                    cartItem.setItem(item);
                    cartItem.setInStock(isInStock);
                    cartItem.setQuantity(item.getQuantity());
                    CartList cartList = new CartList();
                    cartList.setItemid(item.getItemId());
                    cartList.setProductid(item.getProduct().getProductId());
                    cartList.setQuantity(item.getQuantity());
                    cartList.setUsername(username);
                    cartList.setTotal(cartItem.getTotal());
                    cartList.setInStock(String.valueOf(isInStock));
                    cartList.setDescription(item.getProduct().getDescription());
                    catalogService.insertCartList(cartList);
                }
            }
        }
        session.setAttribute("cart",cart);
        request.getRequestDispatcher(VIEW_CART).forward(request,response);
    }
}
